package com.tianyao.compose.baseuse.activity.animate

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.Crossfade
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tianyao.compose.baseuse.R
import com.tianyao.compose.baseuse.theme.ComposeProTheme

/**
 * 大小变化的动画Activity
 */
@OptIn(ExperimentalMaterial3Api::class)
class AnimatedSizeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeProTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    Greeting()
                }
            }
        }
    }


    @Composable
    fun Greeting() {
        //TestAnimatedSize()
        //TestAnimatedCrossfade()
        TestAnimatedContentSize()
    }

    @Composable
    fun TestAnimatedSize() {
        var expanded by remember { mutableStateOf(false) }
        Column(modifier = Modifier
            .fillMaxWidth()
            .height(300.dp)
            .clickable {
                expanded = !expanded
            }) {
            AnimatedContent(targetState = expanded, transitionSpec = {
                //淡入淡出
                fadeIn(animationSpec = tween(1500, 150)) togetherWith
                        fadeOut(animationSpec = tween(1500)) using   //使用using操作符连接SizeTransform
                        SizeTransform { initialSize, targetSize ->
                            if (targetState) {
                                keyframes {   //动画的关键帧
                                    //展开时,先水平方向展开
                                    //在动画的 1500 毫秒处达到目标尺寸
                                    IntSize(targetSize.width, initialSize.height) at 1500
                                    durationMillis = 3000
                                }
                            } else {
                                keyframes {   //动画的关键帧
                                    //收起时,先垂直方向收起
                                    //在动画的 1500 毫秒处达到目标尺寸
                                    IntSize(initialSize.width, targetSize.height) at 1500
                                    durationMillis = 3000
                                }
                            }
                        }
            }, label = "") { targetState ->
                ExpandLayout(targetState)
            }
        }
    }

    @Composable
    private fun ExpandLayout(targetState: Boolean) {
        if (targetState) {
            Text(text = stringResource(id = R.string.verse))
        } else {
            Icon(imageVector = Icons.Filled.AddCircle, contentDescription = null)
        }
    }


    @Composable
    fun TestAnimatedCrossfade() {
        var currentPage by remember { mutableStateOf("A") }
        Crossfade(targetState = currentPage, animationSpec = tween(1500), label = "") { screen ->
            when (screen) {
                "A" -> Text(
                    text = "PAGE LOGIN",
                    fontSize = 26.sp,
                    color = Color.Blue,
                    modifier = Modifier.clickable { currentPage = "B" })

                else -> Text(
                    text = "PAGE MAIN",
                    fontSize = 26.sp,
                    color = Color.Red,
                    modifier = Modifier.clickable { currentPage = "A" })
            }
        }
    }


    @Composable
    fun TestAnimatedContentSize() {
        var expend by remember { mutableStateOf(false) }
        Column(Modifier.padding(16.dp)) {
            Button(
                onClick = { expend = !expend }
            ) {
                Text(if (expend) "Shrink" else "Expand")
            }
            Spacer(Modifier.height(16.dp))
            Box(
                Modifier
                    .background(Color.LightGray)
                    .animateContentSize()
            ) {
                Text(
                    text = stringResource(id = R.string.verse),
                    modifier = Modifier.padding(16.dp),
                    fontSize = 16.sp,
                    textAlign = TextAlign.Justify,
                    maxLines = if (expend) Int.MAX_VALUE else 2
                )
            }
        }
    }


}
